Performance improvements: type stability and reduced allocations#486
Merged
pogudingleb merged 2 commits intoSciML:masterfrom Jan 13, 2026
Merged
Conversation
Optimizations made:
- `extract_coefficients` (util.jl): Fixed type instability with `findfirst`
returning Union{Nothing, Int}. Now uses explicit type-stable loop.
Also pre-computes array lengths outside loops for better performance.
- `monomial_compress` (wronskian.jl): Replaced `Array{Any, 1}` with typed
`Vector{Tuple{P, T}}` for type stability. Pre-computes parameter names
in a Set for O(1) lookup instead of repeated map operations.
- `massive_eval` (wronskian.jl): Uses typed containers (Set{Vector{Int}},
Dict{Vector{Int}, T}) instead of untyped. Pre-allocates working arrays
and uses in-place operations with @inbounds. Pre-sizes result array.
- `det_minor_expansion_inner` (elimination.jl): Replaced `in keys(cache)`
with `haskey(cache)` for better performance. Pre-allocates Sets for
discarded rows/cols. Uses `sort!` on mutable arrays instead of allocating.
These changes improve type stability and reduce unnecessary allocations
in hot code paths, particularly benefiting larger ODE systems.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Collaborator
|
@ChrisRackauckas |
Per reviewer feedback, reverted complex performance-focused changes
that add complexity without making the code cleaner. Kept only
changes that genuinely improve code quality:
Kept:
- haskey() instead of `in keys()` (standard Julia idiom)
- sign = -sign (cleaner)
- Typed arrays Vector{Tuple{P,T}} instead of Array{Any,1}
- Pre-computed param_names Set (avoids repeated map)
- Typed Set{Vector{Int}} and Dict in massive_eval
- Removed commented-out dead code
Reverted:
- Complex pre-allocated loops in det_minor_expansion_inner
- Explicit loops replacing map() in extract_coefficients
- Pre-allocated working arrays and @inbounds loops in massive_eval
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Author
|
@pogudingleb Thanks for the feedback! I've updated the PR to keep only the changes that make the code cleaner/shorter, and reverted the purely performance-focused optimizations. Changes kept (cleaner code):
Changes reverted (added complexity for performance):
Let me know if you'd like any further adjustments! |
Collaborator
|
Thanks, looks good now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR improves performance by fixing type instabilities and reducing allocations in hot code paths:
extract_coefficients(util.jl): Fixed type instability wherefindfirstreturnsUnion{Nothing, Int}. Now uses explicit type-stable loop with::Intassertion. Pre-computes array lengths outside loops.monomial_compress(wronskian.jl): ReplacedArray{Any, 1}with typedVector{Tuple{P, T}}for type stability. Pre-computes parameter names in a Set for O(1) lookup instead of repeated map operations.massive_eval(wronskian.jl): Uses typed containers (Set{Vector{Int}},Dict{Vector{Int}, T}) instead of untyped. Pre-allocates working arrays and uses in-place operations with@inbounds. Pre-sizes result array.det_minor_expansion_inner(elimination.jl): Replacedin keys(cache)withhaskey(cache)for better performance. Pre-allocates Sets for discarded rows/cols. Usessort!on mutable arrays instead of allocating.Benchmarks
Tested on Lotka-Volterra and SIWR models - all tests pass with identical results. The optimizations particularly benefit larger ODE systems where these hot paths are called many times.
Test plan
extract_coefficientsunit test passesmonomial_compressreturns correct number of termscc @ChrisRackauckas
🤖 Generated with Claude Code